package org.csu.mypetstore.web.servlets.Ajax;

import com.alibaba.fastjson.JSON;
import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class ShowWindowWhenMouseHoverServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String categoryId = req.getParameter("CategoryId");
        CatalogService service = new CatalogService();
        List<Product> products= service.getProductListByCategory(categoryId);

        String response = "Product ID      Name\n";
        int i = 0;
        while(i<products.size()){
            Product product = products.get(i);
            response += product.getProductId()+ "      "+product.getName()+"\n";
            i++;
        }

        resp.setContentType("text/json");
        PrintWriter out = resp.getWriter();

        String str = JSON.toJSONString(response);
        out.print(str);
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
